<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/master' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>营销管理</el-breadcrumb-item>
                <el-breadcrumb-item>广告管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="advManFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="advManFormData.aname" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="findName()">查询</el-button>
                    <el-button type="primary" @click="insertAdvMan">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="aname"
                            label="投放平台名称"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="advPosition.name"
                            label="位置">
                    </el-table-column>
                    <el-table-column
                            prop="pic"
                            label="图片">
                        <template slot-scope="scope">
                            <img :src="scope.row.pic" width="100" height="80" align="center">
                        </template>
                    </el-table-column>

                    <el-table-column
                            prop="startTime"
                            label="开始时间">
                    </el-table-column>
                    <el-table-column
                            prop="endTime"
                            label="结束时间">
                    </el-table-column>

                    <el-table-column
                            prop="status"
                            label="上下线状态">
                        <template slot-scope="scope">
                            <el-switch
                                    v-model="scope.row.status"
                                    active-color="#13ce66"
                                    :active-value="0"
                                    :inactive-value="1"
                                    inactive-color="#ff4949"
                                    @change="switchStatus(scope.row.id,$event)">
                            </el-switch>
                        </template>
                    </el-table-column>

                    <el-table-column
                            prop="clickCount"
                            label="点击数">
                    </el-table-column>
                    <el-table-column
                            prop="orderCount"
                            label="下单数">
                    </el-table-column>
                    <el-table-column
                            prop="url"
                            label="网站">
                    </el-table-column>
                    <el-table-column
                            prop="note"
                            label="备注">
                    </el-table-column>
<!--                    <el-table-column-->
<!--                            prop="sort"-->
<!--                            label="排序">-->
<!--                    </el-table-column>-->

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" @click="selById(scope.row.id)" circle></el-button>
                            <el-button type="danger" icon="el-icon-delete" @click="del(scope.row.id)" circle></el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!-- 分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>

        <!--        更新信息表单-->
        <el-dialog
                title="添加广告管理信息"
                :visible.sync="editAdvMan"
                width="30%"
        :before-close="cha"
        >
            <!--          model表单数据-->
            <el-form  label-width="80px" :model="advManForm" :rules="advManFormRules" ref="advManFormRef">
                <el-form-item label="投放平台名称" prop="aname">
                    <el-input v-model="advManForm.aname" blur="" ></el-input>
                </el-form-item>
<!--                <el-form-item label="位置" prop="positionId">-->
<!--                    <el-input v-model="advManForm.positionId"></el-input>-->
<!--                </el-form-item>-->

                <el-form-item label="位置" prop="positionId">
                    <el-select v-model="advManForm.positionId"  placeholder="请选择">
                        <el-option
                                v-for="item in options"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="图片" prop="pic">

                    <el-upload
                            class="avatar-uploader"
                            action="http://localhost:80/marketing/manage/upPicture"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload"
                            :headers="{'token':token}">
                        <img v-if="advManForm.pic" :src="advManForm.pic" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>


<!--                <el-form-item label="开始时间" prop="startTime">-->
<!--                    <el-date-picker-->
<!--                            v-model="advManForm.startTime"-->
<!--                            type="datetime"-->
<!--                            placeholder="选择日期时间">-->
<!--                    </el-date-picker>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="结束时间" prop="endTime">-->
<!--                    <el-date-picker-->
<!--                            v-model="advManForm.endTime"-->
<!--                            type="datetime"-->
<!--                            placeholder="选择日期时间">-->
<!--                    </el-date-picker>-->
<!--                </el-form-item>-->
<!--                测试-->
                    <el-form-item label="开始时间" prop="startTime">
                        <el-date-picker
                                v-model="advManForm.startTime"
                                type="datetime"
                                value-format="yyyy-MM-ddTHH:mm:ss"
                                placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="结束时间" prop="endTime">
                        <el-date-picker
                                v-model="advManForm.endTime"
                                type="datetime"
                                value-format="yyyy-MM-ddTHH:mm:ss"
                                placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>


                <el-form-item label="选择状态" prop="status">
                    <el-radio-group v-model="advManForm.status">
                        <el-radio :label="0">上线</el-radio>
                        <el-radio :label="1">下线</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="点击数" prop="clickCount">
                    <el-input v-model="advManForm.clickCount"></el-input>
                </el-form-item>
                <el-form-item label="下单数" prop="orderCount">
                    <el-input v-model="advManForm.orderCount"></el-input>
                </el-form-item>
                <el-form-item label="路径" prop="url">
                    <el-input placeholder="请输入路径" v-model="advManForm.url">
                        <template slot="prepend">Http://</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="备注" prop="note">
                    <el-input v-model="advManForm.note"></el-input>
                </el-form-item>
<!--                <el-form-item label="排序" prop="sort">-->
<!--                    <el-input v-model="advManForm.sort"></el-input>-->
<!--                </el-form-item>-->
            </el-form>
            <span slot="footer" class="dialog-footer">
                        <el-button @click="qx">取 消</el-button>
                        <el-button type="primary" @click="editAdvManBtn">确 定</el-button>
                    </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "AdverManage",
        data(){
            let selectNameOne = (rule, value, callback) => {
                //查询测试批次号是否重复
                this.$http.post("marketing/manage/selectOneName",this.advManForm).then(res => {
                    console.log(res);
                    if (res.data.code== 5000) {
                        callback(res.data.message);
                    }else {
                        callback();
                    }
                })
            }
            let varTime=(rule, value, callback)=>{
                if (this.advManForm.startTime>this.advManForm.endTime){
                    return callback(new Error("结束时间不能早于开始时间"))
                }else {
                    return callback();
                }

            }
            return {
                //获取令牌
                token: "",
                //图片
                pic:"",
                //条件搜索对象
                advManFormData:{},
                //表格数据
                tableData:[],
                //每页显示的条数
                pageSize: 5,
                //当前的显示的页码
                currentPage:1,
                //总条数
                total:0,
                //更新用户弹出框显示状态
                editAdvMan:false,
                //添加用户表单信息,和加校验
                advManForm:{
                    aname:"",
                    positionId:"",
                    pic:"",
                    startTime:"",
                    endTime:"",
                    status:"",
                    clickCount:"",
                    orderCount:"",
                    url:"",
                    note:"",
                    // sort:"",
                    delivery:"",
                    label:"",
                },
                options:'',
                // 用户表单校验数据:
                advManFormRules:{
                    aname:[{required:true,message:"名称不能为空",trigger:'blur'},
                         {validator:selectNameOne,trigger: 'blur'}],
                    positionId:[{ required: true, message: '位置不可为空',trigger: 'blur'}],
                    pic:[{ required: true, message: '图片不可为空',trigger: 'blur'}],
                    // startTime:[{ required: true, message: '开始时间不可为空',trigger: 'blur'},
                    //     {validator:varTime,trigger: 'blur'}
                    // ],
                    endTime:[{ required: true, message: '结束时间不可为空',trigger: 'blur'},
                        {validator:varTime,trigger: 'blur'}
                    ],

                    clickCount:[{ required: true, message: '点击数不可为空',trigger: 'blur'}],
                    orderCount:[{ required: true, message: '下单数不可为空',trigger: 'blur'}],

                    sort:[{ required: true, message: '排序不可为空',trigger: 'blur'}],
                },
            }
        },
        created() {
            //获取用户的信息
            // this.initAdvMan();
            this.gettoken();
            this.findAllId();
            this.findName();

        },
        methods:{
            gettoken(){
                this.token = sessionStorage.getItem("token");
            },
            //界面状态按钮
            switchStatus(id,status){
                this.$http.put(`/marketing/manage/status/${id}/${status}`).then(function (re) {
                             console.log(re);
                })
            },

            //关联查询
            findName(){
                var that=this;
                this.$http.post(`/marketing/manage/findName/${this.currentPage}/${this.pageSize}`,this.advManFormData).then(function (re) {
                   console.log("1111")
                    console.log(re)

                    that.tableData=re.data.result.records;
                    that.total=re.data.result.total;
                })
            },

            //当改变没有显示的条数时触发该方法
            handleSizeChange(v){
                this.pageSize=v
                // this.initAdvMan();
                this.findName();
            },
            //切换页码时触发该方法
            handleCurrentChange(v){
                this.currentPage=v
                // this.initAdvMan();
                this.findName();
            },
            //点击添加按钮
            insertAdvMan(){
                this.editAdvMan=true;
                //将form表单数据清空,以防止修改时数据回显后表单中有数据冗余
                // this.advManForm={};

            },


            //更新用户弹出框确定按钮事件
            editAdvManBtn(){
                var that = this;
                this.$refs.advManFormRef.validate((valid) => {
                    if (valid) {
                        if (this.advManForm.id != null) {
                            this.$http.put(`/marketing/manage/updateAdvManage`, this.advManForm).then(function (r) {
                                that.$message.success(r.data.message);
                                that.editAdvMan = false;
                                // that.initAdvMan();
                                that.findName();
                            })
                        } else {
                            this.$http.post(`/marketing/manage/insertAdvManage`, this.advManForm).then(function (r) {
                                that.$message.success(r.data.message);
                                that.editAdvMan = false;
                                // that.initAdvMan();
                                that.findName();
                            })
                        }
                        this.$refs.advManFormRef.resetFields();
                    }
                })
            },

            //点击修改根据id进行回显
            selById(id){
                var that=this;
                this.editAdvMan=true;
                this.$http.get(`/marketing/manage/byId/${id}`).then(function (r) {
                    that.advManForm=r.data.result.advMange;
                })
            },
            del(id){
                var that=this;
                this.$http.delete(`/marketing/manage/deleteAdvManage/${id}`).then(function (r) {
                    // that.initAdvMan();
                    that.findName();
                    // location.reload();
                })
                this.currentPage=1;
            },
            //弹出框的取消按钮,重置数据,关闭弹窗
            qx(){
                // this.$refs.advManFormRef.resetFields();
                this.editAdvMan=false;
            },
            //×按钮
            cha(){
                this.$refs.advManFormRef.resetFields();
                this.editAdvMan=false;
            },
            //位置下拉
            findAllId(){
                var that=this;
                this.$http.post(`/marketing/position/findAllId`).then(function(resp){
                    // console.log(resp);
                    that.options=resp.data;

                });
            },
            //上传图片
            //上传路径
            handleAvatarSuccess(res,file) {
                this.advManForm.pic =res;
            },
//图片上传之前确认规格的方法
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg'||'image/jpg'||'image/png';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        }
    }
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

</style>